package com.yike.order.lkl.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lkl.laop.sdk.LKLSDK;
import com.lkl.laop.sdk.exception.SDKException;
import com.lkl.laop.sdk.request.V3LabsQueryTradequeryRequest;
import com.lkl.laop.sdk.request.V3LabsRelationRefundRequest;
import com.lkl.laop.sdk.request.V3LabsTransMicropayEncryRequest;
import com.lkl.laop.sdk.request.V3LabsTransMicropayRequest;
import com.lkl.laop.sdk.request.model.V3LabsTradeLocationInfo;
import com.yike.common.Response.LklPayQueryResponse;
import com.yike.common.Response.LklPayResponse;
import com.yike.common.Response.LklRefundResponse;
import com.yike.common.Response.LklResponse;
import com.yike.common.tool.IpUtil;
import com.yike.common.tool.StringUtil;
import com.yike.order.entity.Order;
import com.yike.order.lkl.demo.BaseCommonDemo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Map;


@Slf4j
@Component
public class V3ApiLabs  extends BaseCommonDemo {


    @Value("${lkl.not_url}")
    private String  notUrl;
    /**
     *
     * @param order //订单信息
     * @param shopDetails //店铺信息
     * @param authCode // 付款码信息
     * @return LklPayResponse
     * @throws Exception
     */
    public LklPayResponse LabsTransMicroPay(Order order, Map<String, Object> shopDetails, String authCode) throws SDKException {
        V3LabsTransMicropayRequest req = new V3LabsTransMicropayRequest();
        req.setTermNo(shopDetails.containsKey("termNo")?shopDetails.get("termNo").toString():"");// 终端号
        req.setMerchantNo(shopDetails.containsKey("externalCustomerNo")?shopDetails.get("externalCustomerNo").toString():"");//外部商户号
        req.setOutTradeNo(order.getOrderNo());// 商户订单号
        req.setAuthCode(authCode); // 付款码信息
        Long amount = order.getAmount().multiply(new BigDecimal(100)).longValue();
        req.setTotalAmount(amount.toString()); // 金额 (分)
//        String Location = shopDetails.get("latitude") + "," + shopDetails.get("longitude");
        req.setLocationInfo(new V3LabsTradeLocationInfo( IpUtil.getRequestIp(), "", "")); // 位置信息
        req.setSettleType("1"); //分账

        req.setNotifyUrl(this.notUrl + "/api-order/lkl/notice/lklCallback");


        log.info("发起支付，orderNo:{},请求参数：{}",order.getOrderNo(), req.toBody());

        //3. 发送请求
        String response = LKLSDK.httpPost(req);
        log.info("发起支付-返回内容，response:{}",response);

        LklResponse lklResponse = JSONObject.parseObject(response,LklResponse.class);
        if(!lklResponse.getCode().equals("BBS00000") && !lklResponse.getCode().equals("BBS11105") && !lklResponse.getCode().equals("BBS10000")){
            throw new RuntimeException(lklResponse.getMsg());
        }

        return JSON.parseObject(JSON.toJSONString(lklResponse.getRespData()), LklPayResponse.class);
    }








    /**
     * 查询订单支付结果
     * @param merchantNo // 商户号
     * @param termNo // 终端号
     * @param outTradeNo // 商户订单号
     * @param tradeNo // 支付渠道订单号
     * @return
     */
    public static LklPayQueryResponse tradeQuery(String merchantNo , String termNo , String outTradeNo , String tradeNo) throws SDKException {
        V3LabsQueryTradequeryRequest req = new V3LabsQueryTradequeryRequest();
        req.setMerchantNo(merchantNo);
        req.setTermNo(termNo);
        req.setOutTradeNo(outTradeNo);
        if(StringUtil.isNotBlank(tradeNo)){
            req.setTradeNo(tradeNo);
        }
        log.info("lkl查询订单请求参数：{}",JSON.toJSONString(req));

        req.setOutTradeNo(outTradeNo);

        //3. 发送请求
        String response = LKLSDK.httpPost(req);
        log.info("lkl查询订单返回结果：{}",response);

        LklResponse lklResponse = JSONObject.parseObject(response,LklResponse.class);
        if(!lklResponse.getCode().equals("BBS00000")){
            throw new RuntimeException(lklResponse.getMsg());
        }

        return JSON.parseObject(JSON.toJSONString(lklResponse.getRespData()), LklPayQueryResponse.class);
    }






    /**
     * 订单退款
     * @param merchantNo // 商户号
     * @param termNo // 终端号
     * @param outTradeNo // 商户订单号
     * @param refundTradeNo // 退款单号
     * @param originTradeNo // 支付渠道订单号
     * @param refundAmount // 退款金额
     * @return LklRefundResponse
     */
    public static LklRefundResponse refund(String merchantNo , String termNo , String outTradeNo ,String refundTradeNo, String originTradeNo , String refundAmount) throws SDKException {
        V3LabsRelationRefundRequest req = new V3LabsRelationRefundRequest();
        req.setMerchantNo(merchantNo);
        req.setTermNo(termNo);
        req.setOutTradeNo(refundTradeNo);
        req.setOriginOutTradeNo(outTradeNo);

        req.setOriginTradeNo(originTradeNo);
        req.setRefundAmount(refundAmount);
        req.setRefundReason("退款");
        req.setLocationInfo(new V3LabsTradeLocationInfo( IpUtil.getRequestIp(), "", "")); // 位置信息

        log.info("lkl申请退款订单请求参数：{}",JSON.toJSONString(req));

        //3. 发送请求
        String response = LKLSDK.httpPost(req);
        log.info("lkl申请退款订单返回结果：{}",response);

        LklResponse lklResponse = JSONObject.parseObject(response,LklResponse.class);
        if(!lklResponse.getCode().equals("BBS00000")){
            throw new RuntimeException(lklResponse.getMsg());
        }

        return JSON.parseObject(JSON.toJSONString(lklResponse.getRespData()), LklRefundResponse.class);
    }
}
